<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.10"/>
<title>Introduction_to_Algorithms: src/dynamic_programming_algorithms/lcs/longest_common_subsequence.h Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Introduction_to_Algorithms
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.10 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('longest__common__subsequence_8h_source.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">longest_common_subsequence.h</div>  </div>
</div><!--header-->
<div class="contents">
<a href="longest__common__subsequence_8h.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="comment">/*</span></div>
<div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="comment"> * Copyright 2016- huaxz &lt;huaxz1986@163.com&gt;</span></div>
<div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="comment"> * This program is free software: you can redistribute it and/or modify</span></div>
<div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="comment"> * it under the terms of the GNU General Public License as published by</span></div>
<div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="comment"> * the Free Software Foundation, either version 2 of the License, or</span></div>
<div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;<span class="comment"> * (at your option) any later version.</span></div>
<div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="comment"> * This program is distributed in the hope that it will be useful,</span></div>
<div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;<span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span></div>
<div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;<span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span></div>
<div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;<span class="comment"> * GNU General Public License for more details.</span></div>
<div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;<span class="comment"> * You should have received a copy of the GNU General Public License</span></div>
<div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;<span class="comment"> * along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span></div>
<div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;<span class="comment"> * Author: huaxz1986@163.com (huaxz)</span></div>
<div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;<span class="preprocessor">#ifndef LONGESTCOMMONSUBSEQUENCE</span></div>
<div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;<span class="preprocessor">#define LONGESTCOMMONSUBSEQUENCE</span></div>
<div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;<span class="preprocessor">#include &lt;type_traits&gt;</span></div>
<div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;<span class="preprocessor">#include&lt;vector&gt;</span></div>
<div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;<span class="preprocessor">#include&lt;iostream&gt;</span></div>
<div class="line"><a name="l00024"></a><span class="lineno"><a class="line" href="namespace_introduction_to_algorithm.html">   24</a></span>&#160;<span class="keyword">namespace  </span><a class="code" href="namespace_introduction_to_algorithm.html">IntroductionToAlgorithm</a> {</div>
<div class="line"><a name="l00025"></a><span class="lineno"><a class="line" href="namespace_introduction_to_algorithm_1_1_dynamic_programming_algorithm.html">   25</a></span>&#160;    <span class="keyword">namespace </span>DynamicProgrammingAlgorithm</div>
<div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;    {</div>
<div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;</div>
<div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;</div>
<div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;    <span class="keyword">template</span>&lt;<span class="keyword">typename</span> Iterator,<span class="keyword">typename</span> OutIterator&gt; std::size_t</div>
<div class="line"><a name="l00062"></a><span class="lineno"><a class="line" href="namespace_introduction_to_algorithm_1_1_dynamic_programming_algorithm.html#a377ef4206d4814024272c15c51051f09">   62</a></span>&#160;                      <a class="code" href="namespace_introduction_to_algorithm_1_1_dynamic_programming_algorithm.html#a377ef4206d4814024272c15c51051f09">make_LCS</a>(<span class="keyword">const</span> Iterator begin,<span class="keyword">const</span> Iterator end,</div>
<div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;                      <span class="keyword">const</span> std::vector&lt;std::vector&lt;int&gt;&gt;&amp; flag_matrix,<span class="keyword">typename</span> std::iterator_traits&lt;Iterator&gt;::difference_type seq1_index,</div>
<div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;                              <span class="keyword">typename</span> std::iterator_traits&lt;Iterator&gt;::difference_type seq2_index,</div>
<div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;                               OutIterator&amp; out_begin)</div>
<div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;    {</div>
<div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;        <span class="keyword">typedef</span> <span class="keyword">typename</span> std::iterator_traits&lt;Iterator&gt;::value_type T;</div>
<div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;        <span class="keyword">typedef</span> <span class="keyword">typename</span> std::iterator_traits&lt;OutIterator&gt;::value_type Out_T;</div>
<div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;        static_assert(std::is_same&lt;T, Out_T&gt;::value,<span class="stringliteral">&quot;输入序列与输出序列必须包含相同类型的元素&quot;</span>);</div>
<div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;        std::size_t result=0;</div>
<div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160;        <span class="keywordflow">if</span>(seq1_index&lt;0||seq2_index&lt;0)</div>
<div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;            <span class="keywordflow">return</span> result;</div>
<div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160;        <span class="keywordflow">if</span>(flag_matrix[seq1_index][seq2_index]==11)  <span class="comment">//两个子序列尾部相同</span></div>
<div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;        {</div>
<div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;            result+=(<a class="code" href="namespace_introduction_to_algorithm_1_1_dynamic_programming_algorithm.html#a377ef4206d4814024272c15c51051f09">make_LCS</a>(begin,end,flag_matrix,seq1_index-1,seq2_index-1,out_begin)+1);</div>
<div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;            *out_begin=*(begin+seq1_index); <span class="comment">//由于是从尾部向前打印，这里要递归调用之后输出</span></div>
<div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160;            out_begin++; <span class="comment">//这里修改了out_begin,从而在不同迭代次数下，其值会发生改变</span></div>
<div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;            <span class="keywordflow">return</span> result;</div>
<div class="line"><a name="l00079"></a><span class="lineno">   79</span>&#160;        }<span class="keywordflow">else</span> <span class="keywordflow">if</span>(flag_matrix[seq1_index][seq2_index]==10)<span class="comment">//表示c[i-1,j]较大，此时X[0...i]与Y[0...j]最长公共子序列也是X[0...i-1]与Y[0...j]最长公共子序列</span></div>
<div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;        {</div>
<div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;            result+=<a class="code" href="namespace_introduction_to_algorithm_1_1_dynamic_programming_algorithm.html#a377ef4206d4814024272c15c51051f09">make_LCS</a>(begin,end,flag_matrix,seq1_index-1,seq2_index,out_begin);</div>
<div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160;            <span class="keywordflow">return</span> result;</div>
<div class="line"><a name="l00083"></a><span class="lineno">   83</span>&#160;        }<span class="keywordflow">else</span>       <span class="comment">//表示c[i,j-1]较大，此时X[0...i]与Y[0...j]最长公共子序列也是X[0...i]与Y[0...j-1]最长公共子序列</span></div>
<div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;        {</div>
<div class="line"><a name="l00085"></a><span class="lineno">   85</span>&#160;            result+= <a class="code" href="namespace_introduction_to_algorithm_1_1_dynamic_programming_algorithm.html#a377ef4206d4814024272c15c51051f09">make_LCS</a>(begin,end,flag_matrix,seq1_index,seq2_index-1,out_begin);</div>
<div class="line"><a name="l00086"></a><span class="lineno">   86</span>&#160;            <span class="keywordflow">return</span> result;</div>
<div class="line"><a name="l00087"></a><span class="lineno">   87</span>&#160;        }</div>
<div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;    }</div>
<div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;</div>
<div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160;</div>
<div class="line"><a name="l00112"></a><span class="lineno">  112</span>&#160;        <span class="keyword">template</span>&lt;<span class="keyword">typename</span> Iterator1,<span class="keyword">typename</span> Iterator2,<span class="keyword">typename</span> OutIterator&gt;  std::size_t</div>
<div class="line"><a name="l00113"></a><span class="lineno"><a class="line" href="namespace_introduction_to_algorithm_1_1_dynamic_programming_algorithm.html#a02edd40c3c2dd3ce1b514284dd42e99c">  113</a></span>&#160;                <a class="code" href="namespace_introduction_to_algorithm_1_1_dynamic_programming_algorithm.html#a02edd40c3c2dd3ce1b514284dd42e99c">longest_common_subsequence</a>(<span class="keyword">const</span> Iterator1 first_begin,<span class="keyword">const</span> Iterator1 first_end,</div>
<div class="line"><a name="l00114"></a><span class="lineno">  114</span>&#160;                                           <span class="keyword">const</span> Iterator2 second_begin,<span class="keyword">const</span> Iterator2 second_end,</div>
<div class="line"><a name="l00115"></a><span class="lineno">  115</span>&#160;                                           OutIterator out_begin)</div>
<div class="line"><a name="l00116"></a><span class="lineno">  116</span>&#160;        {</div>
<div class="line"><a name="l00117"></a><span class="lineno">  117</span>&#160;            <span class="keyword">typedef</span> <span class="keyword">typename</span> std::iterator_traits&lt;Iterator1&gt;::value_type T1;</div>
<div class="line"><a name="l00118"></a><span class="lineno">  118</span>&#160;            <span class="keyword">typedef</span> <span class="keyword">typename</span> std::iterator_traits&lt;Iterator2&gt;::value_type T2;</div>
<div class="line"><a name="l00119"></a><span class="lineno">  119</span>&#160;            <span class="keyword">typedef</span> <span class="keyword">typename</span> std::iterator_traits&lt;OutIterator&gt;::value_type T3;</div>
<div class="line"><a name="l00120"></a><span class="lineno">  120</span>&#160;            <span class="comment">//typedef typename std::iterator_traits&lt;Iterator1&gt;::difference_type Distance;</span></div>
<div class="line"><a name="l00121"></a><span class="lineno">  121</span>&#160;            static_assert(std::is_same&lt;T1, T2&gt;::value,<span class="stringliteral">&quot;两个序列必须包含相同类型的元素&quot;</span>);</div>
<div class="line"><a name="l00122"></a><span class="lineno">  122</span>&#160;            static_assert(std::is_same&lt;T1, T3&gt;::value,<span class="stringliteral">&quot;输入序列与输出序列必须包含相同类型的元素&quot;</span>);</div>
<div class="line"><a name="l00123"></a><span class="lineno">  123</span>&#160;            <span class="keyword">auto</span> len1=std::distance(first_begin,first_end);</div>
<div class="line"><a name="l00124"></a><span class="lineno">  124</span>&#160;            <span class="keyword">auto</span> len2=std::distance(second_begin,second_end);</div>
<div class="line"><a name="l00125"></a><span class="lineno">  125</span>&#160;            <span class="keywordflow">if</span>(len1&lt;=0||len2&lt;=0)</div>
<div class="line"><a name="l00126"></a><span class="lineno">  126</span>&#160;                <span class="keywordflow">return</span> 0;</div>
<div class="line"><a name="l00127"></a><span class="lineno">  127</span>&#160;            <span class="keyword">auto</span> rows=len1;</div>
<div class="line"><a name="l00128"></a><span class="lineno">  128</span>&#160;            <span class="keyword">auto</span> columns=len2;</div>
<div class="line"><a name="l00129"></a><span class="lineno">  129</span>&#160;            <span class="comment">//*******************  创建数据矩阵  *******************</span></div>
<div class="line"><a name="l00130"></a><span class="lineno">  130</span>&#160;            std::vector&lt;std::vector&lt;int&gt;&gt; c_matrix(rows+1); <span class="comment">//c_matrix[i,j]为Xi和Yj的最长公共子序列长度，因为i,j可能为0，所以矩阵外扩一个单位</span></div>
<div class="line"><a name="l00131"></a><span class="lineno">  131</span>&#160;            <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0;i&lt;rows+1;i++)  <span class="comment">// 矩阵大小 (rows+1)*(colums+1)</span></div>
<div class="line"><a name="l00132"></a><span class="lineno">  132</span>&#160;            {</div>
<div class="line"><a name="l00133"></a><span class="lineno">  133</span>&#160;                <span class="comment">/*</span></div>
<div class="line"><a name="l00134"></a><span class="lineno">  134</span>&#160;<span class="comment">                 * 这里大小为什么为 (m+1)*(n+1)：因为X序列的子序列可以为 空、&lt;x1&gt;、...&lt;x1...xm&gt;一共（m+1）个；Y序列的子序列有 (n+1)个</span></div>
<div class="line"><a name="l00135"></a><span class="lineno">  135</span>&#160;<span class="comment">                 */</span></div>
<div class="line"><a name="l00136"></a><span class="lineno">  136</span>&#160;                c_matrix.at(i)=std::vector&lt;int&gt;(columns+1);</div>
<div class="line"><a name="l00137"></a><span class="lineno">  137</span>&#160;            }</div>
<div class="line"><a name="l00138"></a><span class="lineno">  138</span>&#160;            std::vector&lt;std::vector&lt;int&gt;&gt; flag_matrix(rows); <span class="comment">//用于构造最长公共子序列，它存放的是从c[i-1][j-1]到c[i,j]的路径</span></div>
<div class="line"><a name="l00139"></a><span class="lineno">  139</span>&#160;            <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0;i&lt;rows;i++)<span class="comment">// 矩阵大小 (rows)*(colums)</span></div>
<div class="line"><a name="l00140"></a><span class="lineno">  140</span>&#160;            {</div>
<div class="line"><a name="l00141"></a><span class="lineno">  141</span>&#160;                <span class="comment">/*</span></div>
<div class="line"><a name="l00142"></a><span class="lineno">  142</span>&#160;<span class="comment">                 * 这这里大小为什么为m*n:因为它表征的是如何从c[i-1,j-1]、c[i,j-1]、c[i-1,j]这三者之一到达c[i,j]。即flag_matrix（i,j)对应的是矩阵：</span></div>
<div class="line"><a name="l00143"></a><span class="lineno">  143</span>&#160;<span class="comment">                 *    | c[i][j]    c[i+1][j]    |</span></div>
<div class="line"><a name="l00144"></a><span class="lineno">  144</span>&#160;<span class="comment">                 *    |                         |</span></div>
<div class="line"><a name="l00145"></a><span class="lineno">  145</span>&#160;<span class="comment">                 *    | c[i+1][j]  c[i+1][j+1]  |</span></div>
<div class="line"><a name="l00146"></a><span class="lineno">  146</span>&#160;<span class="comment">                 */</span></div>
<div class="line"><a name="l00147"></a><span class="lineno">  147</span>&#160;                flag_matrix.at(i)=std::vector&lt;int&gt;(columns);</div>
<div class="line"><a name="l00148"></a><span class="lineno">  148</span>&#160;            }</div>
<div class="line"><a name="l00149"></a><span class="lineno">  149</span>&#160;            <span class="comment">//**************　　初始化矩阵　****************************</span></div>
<div class="line"><a name="l00150"></a><span class="lineno">  150</span>&#160;            <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0;i&lt;rows+1;i++)  <span class="comment">// c[i,j]= 0（若 i=0或j=0)</span></div>
<div class="line"><a name="l00151"></a><span class="lineno">  151</span>&#160;                   c_matrix[i][0]=0;</div>
<div class="line"><a name="l00152"></a><span class="lineno">  152</span>&#160;            <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0;i&lt;columns+1;i++)<span class="comment">// c[i,j]= 0（若 i=0或j=0)</span></div>
<div class="line"><a name="l00153"></a><span class="lineno">  153</span>&#160;                   c_matrix[0][i]=0;</div>
<div class="line"><a name="l00154"></a><span class="lineno">  154</span>&#160;            <span class="comment">//********************* 开始计算 ********************</span></div>
<div class="line"><a name="l00155"></a><span class="lineno">  155</span>&#160;            <span class="keywordflow">for</span>(<span class="keywordtype">int</span> r=1;r&lt;rows+1;r++) <span class="comment">//row=0,column=0已由初始化条件给出，这里从r=1,c=1开始</span></div>
<div class="line"><a name="l00156"></a><span class="lineno">  156</span>&#160;                <span class="keywordflow">for</span>(<span class="keywordtype">int</span> c=1;c&lt;columns+1;c++)</div>
<div class="line"><a name="l00157"></a><span class="lineno">  157</span>&#160;                {</div>
<div class="line"><a name="l00158"></a><span class="lineno">  158</span>&#160;                       <span class="keywordflow">if</span>( *(first_begin+r-1) ==*(second_begin+c-1))<span class="comment">//c[i,j]=c[i-1,j-1]+1 (若i,j&gt;0,且xi=yj)</span></div>
<div class="line"><a name="l00159"></a><span class="lineno">  159</span>&#160;                       {</div>
<div class="line"><a name="l00160"></a><span class="lineno">  160</span>&#160;                                c_matrix[r][c]=c_matrix[r-1][c-1]+1;</div>
<div class="line"><a name="l00161"></a><span class="lineno">  161</span>&#160;                                flag_matrix[r-1][c-1]=11;  <span class="comment">//c[i,j]=c[i-1,j-1]+1，标记flag_matrix[i-1][j-1] 为11</span></div>
<div class="line"><a name="l00162"></a><span class="lineno">  162</span>&#160;</div>
<div class="line"><a name="l00163"></a><span class="lineno">  163</span>&#160;                       }<span class="keywordflow">else</span> <span class="keywordflow">if</span>(c_matrix[r-1][c]&gt;=c_matrix[r][c-1])<span class="comment">//c[i,j]=max(c[i,j-1],c[i-1,j]) (若i,j&gt;0,且xi=yj)</span></div>
<div class="line"><a name="l00164"></a><span class="lineno">  164</span>&#160;                       {</div>
<div class="line"><a name="l00165"></a><span class="lineno">  165</span>&#160;                                c_matrix[r][c]=c_matrix[r-1][c];</div>
<div class="line"><a name="l00166"></a><span class="lineno">  166</span>&#160;                                flag_matrix[r-1][c-1]=10; <span class="comment">// 标记flag_matrix[i-1][j-1] 为10，表示c[i-1,j]较大</span></div>
<div class="line"><a name="l00167"></a><span class="lineno">  167</span>&#160;                       }<span class="keywordflow">else</span>                   <span class="comment">//c[i,j]=max(c[i,j-1],c[i-1,j]) (若i,j&gt;0,且xi=yj)</span></div>
<div class="line"><a name="l00168"></a><span class="lineno">  168</span>&#160;                       {</div>
<div class="line"><a name="l00169"></a><span class="lineno">  169</span>&#160;                                c_matrix[r][c]=c_matrix[r][c-1];</div>
<div class="line"><a name="l00170"></a><span class="lineno">  170</span>&#160;                                flag_matrix[r-1][c-1]=1;<span class="comment">// 标记flag_matrix[i-1][j-1] 为01，表示c[i,j-1]较大</span></div>
<div class="line"><a name="l00171"></a><span class="lineno">  171</span>&#160;                        }</div>
<div class="line"><a name="l00172"></a><span class="lineno">  172</span>&#160;                 }</div>
<div class="line"><a name="l00173"></a><span class="lineno">  173</span>&#160;            <span class="comment">// 打印矩阵</span></div>
<div class="line"><a name="l00174"></a><span class="lineno">  174</span>&#160;<span class="comment">//            std::cout&lt;&lt;&quot;j: &quot;;</span></div>
<div class="line"><a name="l00175"></a><span class="lineno">  175</span>&#160;<span class="comment">//            for(int c=0;c&lt;columns+1;c++)</span></div>
<div class="line"><a name="l00176"></a><span class="lineno">  176</span>&#160;<span class="comment">//                std::cout&lt;&lt;&quot;\tc: &quot;&lt;&lt;c;</span></div>
<div class="line"><a name="l00177"></a><span class="lineno">  177</span>&#160;<span class="comment">//            std::cout&lt;&lt;&quot;\n&quot;;</span></div>
<div class="line"><a name="l00178"></a><span class="lineno">  178</span>&#160;<span class="comment">//            for(int r=0;r&lt;rows+1;r++)</span></div>
<div class="line"><a name="l00179"></a><span class="lineno">  179</span>&#160;<span class="comment">//            {</span></div>
<div class="line"><a name="l00180"></a><span class="lineno">  180</span>&#160;<span class="comment">//                std::cout&lt;&lt;&quot;i:&quot;&lt;&lt;r;</span></div>
<div class="line"><a name="l00181"></a><span class="lineno">  181</span>&#160;<span class="comment">//                for(int c=0;c&lt;columns+1;c++)</span></div>
<div class="line"><a name="l00182"></a><span class="lineno">  182</span>&#160;<span class="comment">//                    std::cout&lt;&lt;&quot;\t &quot;&lt;&lt;c_matrix[r][c];</span></div>
<div class="line"><a name="l00183"></a><span class="lineno">  183</span>&#160;<span class="comment">//                std::cout&lt;&lt;&quot;\n&quot;;</span></div>
<div class="line"><a name="l00184"></a><span class="lineno">  184</span>&#160;<span class="comment">//           }</span></div>
<div class="line"><a name="l00185"></a><span class="lineno">  185</span>&#160;<span class="comment">//            std::cout&lt;&lt;&quot;\n\n&quot;;</span></div>
<div class="line"><a name="l00186"></a><span class="lineno">  186</span>&#160;<span class="comment">//            for(int c=0;c&lt;columns;c++)</span></div>
<div class="line"><a name="l00187"></a><span class="lineno">  187</span>&#160;<span class="comment">//                std::cout&lt;&lt;&quot;j:\tc: &quot;&lt;&lt;c;</span></div>
<div class="line"><a name="l00188"></a><span class="lineno">  188</span>&#160;<span class="comment">//            std::cout&lt;&lt;&quot;\n&quot;;</span></div>
<div class="line"><a name="l00189"></a><span class="lineno">  189</span>&#160;<span class="comment">//            for(int r=0;r&lt;rows;r++)</span></div>
<div class="line"><a name="l00190"></a><span class="lineno">  190</span>&#160;<span class="comment">//            {</span></div>
<div class="line"><a name="l00191"></a><span class="lineno">  191</span>&#160;<span class="comment">//                std::cout&lt;&lt;&quot;i:&quot;&lt;&lt;r;</span></div>
<div class="line"><a name="l00192"></a><span class="lineno">  192</span>&#160;<span class="comment">//                for(int c=0;c&lt;columns;c++)</span></div>
<div class="line"><a name="l00193"></a><span class="lineno">  193</span>&#160;<span class="comment">//                    std::cout&lt;&lt;&quot;\t &quot;&lt;&lt;flag_matrix[r][c];</span></div>
<div class="line"><a name="l00194"></a><span class="lineno">  194</span>&#160;<span class="comment">//                std::cout&lt;&lt;&quot;\n&quot;;</span></div>
<div class="line"><a name="l00195"></a><span class="lineno">  195</span>&#160;<span class="comment">//           }</span></div>
<div class="line"><a name="l00196"></a><span class="lineno">  196</span>&#160;</div>
<div class="line"><a name="l00197"></a><span class="lineno">  197</span>&#160;            <span class="comment">//*************  构造最长公共子序列  ****************</span></div>
<div class="line"><a name="l00198"></a><span class="lineno">  198</span>&#160;            <span class="keywordflow">return</span> <a class="code" href="namespace_introduction_to_algorithm_1_1_dynamic_programming_algorithm.html#a377ef4206d4814024272c15c51051f09">make_LCS</a>(first_begin,first_end,flag_matrix,len1-1,len2-1,out_begin); <span class="comment">//从尾部开始向前打印</span></div>
<div class="line"><a name="l00199"></a><span class="lineno">  199</span>&#160;        }</div>
<div class="line"><a name="l00200"></a><span class="lineno">  200</span>&#160;</div>
<div class="line"><a name="l00201"></a><span class="lineno">  201</span>&#160;    }</div>
<div class="line"><a name="l00202"></a><span class="lineno">  202</span>&#160;}</div>
<div class="line"><a name="l00203"></a><span class="lineno">  203</span>&#160;<span class="preprocessor">#endif // LONGESTCOMMONSUBSEQUENCE</span></div>
<div class="line"><a name="l00204"></a><span class="lineno">  204</span>&#160;</div>
<div class="ttc" id="namespace_introduction_to_algorithm_1_1_dynamic_programming_algorithm_html_a02edd40c3c2dd3ce1b514284dd42e99c"><div class="ttname"><a href="namespace_introduction_to_algorithm_1_1_dynamic_programming_algorithm.html#a02edd40c3c2dd3ce1b514284dd42e99c">IntroductionToAlgorithm::DynamicProgrammingAlgorithm::longest_common_subsequence</a></div><div class="ttdeci">std::size_t longest_common_subsequence(const Iterator1 first_begin, const Iterator1 first_end, const Iterator2 second_begin, const Iterator2 second_end, OutIterator out_begin)</div><div class="ttdoc">longest_common_subsequence 算法导论第15章9.4 最长公共子序列 </div><div class="ttdef"><b>Definition:</b> <a href="longest__common__subsequence_8h_source.html#l00113">longest_common_subsequence.h:113</a></div></div>
<div class="ttc" id="namespace_introduction_to_algorithm_html"><div class="ttname"><a href="namespace_introduction_to_algorithm.html">IntroductionToAlgorithm</a></div><div class="ttdoc">Namespace of IntrodunctionToAlgorithm. </div><div class="ttdef"><b>Definition:</b> <a href="longest__common__subsequence_8h_source.html#l00024">longest_common_subsequence.h:24</a></div></div>
<div class="ttc" id="namespace_introduction_to_algorithm_1_1_dynamic_programming_algorithm_html_a377ef4206d4814024272c15c51051f09"><div class="ttname"><a href="namespace_introduction_to_algorithm_1_1_dynamic_programming_algorithm.html#a377ef4206d4814024272c15c51051f09">IntroductionToAlgorithm::DynamicProgrammingAlgorithm::make_LCS</a></div><div class="ttdeci">std::size_t make_LCS(const Iterator begin, const Iterator end, const std::vector&lt; std::vector&lt; int &gt;&gt; &amp;flag_matrix, typename std::iterator_traits&lt; Iterator &gt;::difference_type seq1_index, typename std::iterator_traits&lt; Iterator &gt;::difference_type seq2_index, OutIterator &amp;out_begin)</div><div class="ttdoc">make_LCS 最长公共子序列的子算法：已知标记矩阵，求最长公共子序列 </div><div class="ttdef"><b>Definition:</b> <a href="longest__common__subsequence_8h_source.html#l00062">longest_common_subsequence.h:62</a></div></div>
</div><!-- fragment --></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="dir_8ed9dba4cd616b67da9b1338594e34e0.html">src</a></li><li class="navelem"><a class="el" href="dir_869b05e44de4bf15e2e0a0dbeb81eb52.html">dynamic_programming_algorithms</a></li><li class="navelem"><a class="el" href="dir_b4c4aa7cfd3e6dd4be8b647d7f0a6ea2.html">lcs</a></li><li class="navelem"><a class="el" href="longest__common__subsequence_8h.html">longest_common_subsequence.h</a></li>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.10 </li>
  </ul>
</div>
</body>
</html>
